package cn.study.vue_project_xxx.manager;


import cn.study.vue_project_xxx.common.exception.KnowException;
import cn.study.vue_project_xxx.entity.dto.RegisterUserDTO;
import cn.study.vue_project_xxx.entity.po.TAdmin;
import cn.study.vue_project_xxx.entity.po.UserInfo;
import cn.study.vue_project_xxx.entity.po.UserLogin;
import cn.study.vue_project_xxx.mapper.TAdminMapper;
import cn.study.vue_project_xxx.mapper.UserInfoMapper;
import cn.study.vue_project_xxx.mapper.UserLoginMapper;
import cn.study.vue_project_xxx.util.InstanceFactoryUtil;
import cn.study.vue_project_xxx.util.UidGeneratorUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import static cn.study.vue_project_xxx.common.enumeration.MapKey.*;

@Component
public class TadminManager {

    @Resource
    UserInfoMapper userInfoMapper;

    @Resource
    UserLoginManager userLoginManager;

    @Resource
    UserLoginMapper userLoginMapper;

    @Resource
    TAdminMapper adminMapper;

    /**
     * 保存User信息
     */
    public Integer saveUserInfo(UserInfo userInfo) {
        checkUserInfo(userInfo);
        return userInfoMapper.insert(userInfo);
    }

    private void checkUserInfo(UserInfo userInfo) {
        if (!userInfo.getUserMobile().matches("^1[3|4|5|7|8][0-9]{9}$")) {
            throw new KnowException("手机号码不正确!");
        }
        if (!userInfo.getUserEmail().matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")) {
            throw new KnowException("邮箱不合法!");
        }
        if (Objects.isNull(userInfo.getUserName()) || userInfo.getUserName().equals("")) {
            throw new KnowException("用户名称不合法");
        }
    }


    public HashMap<String,Integer> saveEntity(RegisterUserDTO registerUserDTO, Integer userLevel) {
        // 获取实体用户信息
        UserInfo userInfo = InstanceFactoryUtil.getInstance(UserInfo.class);
        UserLogin userLongin = InstanceFactoryUtil.getInstance(UserLogin.class);

        Long userInfoId = UidGeneratorUtil.generatorUid();

        // 该用户为普通用户
        userInfo.setUserLeve(userLevel);
        userInfo.setUserId(userInfoId);
        userLongin.setLoginUserId(userInfoId.toString());
        // 封装userInfo对象
        BeanUtils.copyProperties(registerUserDTO, userInfo);
        // 封装userLongin对象
        BeanUtils.copyProperties(registerUserDTO, userLongin);

        // 插入用户信息
        Integer userInfoResult = saveUserInfo(userInfo);
        Integer userLoginResult = userLoginManager.saveUserLogin(userLongin);
        HashMap<String, Integer> resultHashMap = new HashMap<>();
        resultHashMap.put(userInfoKey,userInfoResult);
        resultHashMap.put(userLoginKey,userLoginResult);
        resultHashMap.put(userInfoIdKey,userInfoId.intValue());
        return resultHashMap;
    }


    public HashMap<String, Integer> updateAdmin(TAdmin admin) {
        //执行修改   1修改用户信息  2修改登录账号密码相关信息
        LambdaUpdateWrapper<TAdmin> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(TAdmin::getAdminId, admin.getAdminId());
        wrapper.set(!Objects.isNull(admin.getAdminName()),TAdmin::getAdminName, admin.getAdminName());
        wrapper.set(!Objects.isNull(admin.getAdminBirthday()),TAdmin::getAdminBirthday, admin.getAdminBirthday());
        wrapper.set(!Objects.isNull(admin.getAdminMobile()),TAdmin::getAdminMobile, admin.getAdminMobile());
        wrapper.set(!Objects.isNull(admin.getAdminEmail()),TAdmin::getAdminEmail, admin.getAdminEmail());
        wrapper.set(!Objects.isNull(admin.getImageUrl()),TAdmin::getImageUrl, admin.getImageUrl());
        int update = adminMapper.update(admin,wrapper);
        HashMap<String, Integer> map = new HashMap<>();
        map.put(adminKey,update);
        return map;
    }

    public Page<TAdmin> selectPage(Page<TAdmin> page, List<Long> userIds) {
        //执行查询
        LambdaQueryWrapper<TAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TAdmin::getAdminId,userIds);
        return adminMapper.selectPage(page,wrapper);
    }
}
